﻿using System.Collections.Generic;
using Transformable_Engine_v2.Engine.Input;

namespace Platformer_Example.Controller.Input
{
    public class InputTransmitter
    {

        private readonly Dictionary<KeyboardInput.Key, InputState.Key> _keyToActionMapping;

        public InputState PlayerInput { get; private set; }

        private readonly int _gamepadId;

        public InputTransmitter(InputState inputStateListener, Dictionary<KeyboardInput.Key, InputState.Key> keyMapping, int gamepadId = -1)
        {
            _gamepadId = gamepadId;
            _keyToActionMapping = keyMapping;
            PlayerInput = inputStateListener;
        }

        public InputState.Key OnKeyPress(int gamepadId, KeyboardInput.Key keyCode)
        {
            //Wrong control method
            if (gamepadId != _gamepadId)
                return InputState.Key.Invalid;

            if (_keyToActionMapping.ContainsKey(keyCode))
                PlayerInput.PressKey(_keyToActionMapping[keyCode]);
            else
                return InputState.Key.Invalid;

            return _keyToActionMapping[keyCode];

        }

        public InputState.Key OnKeyRelease(int gamepadId, KeyboardInput.Key keyCode)
        {
            //Wrong control method
            if (gamepadId != _gamepadId)
                return InputState.Key.Invalid;

            if (_keyToActionMapping.ContainsKey(keyCode))
                PlayerInput.ReleaseKey(_keyToActionMapping[keyCode]);
            else
                return InputState.Key.Invalid;

            return _keyToActionMapping[keyCode];
        }
    }
}